package com.meihua.shellapp.fragment.policy;


import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.AdapterView;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.wallet.IncomeDetailActivity;
import com.meihua.shellapp.adapter.personalcenter.CurrentMonthFragmentAdapter;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.databean.BillDataBean;
import com.meihua.shellapp.bean.MonthListBean;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;

/**
 * 我的钱包 当月收入
 */
public class CurrentMonthFragment extends BaseFragment {
    @Bind(R.id.rv)
    IRecyclerView rv;

    private int type = 1;
    private String url;
    private LoadMoreFooterView loadMoreFooterView;
    private List<BillDataBean> mData = new ArrayList<>();
    private CurrentMonthFragmentAdapter mAdapter;
    private int mPage = 1;

    /**
     * 设置类型
     *
     * @param type 1.当月收入   2.提现记录
     * @return
     */
    public CurrentMonthFragment setType(int type) {
        this.type = type;
        if (type == 1) {
            url = URL.getUrl().MONTHLIST;
        } else if (type == 2) {
            url = URL.getUrl().GETSALARY;
        } else {
            url = URL.getUrl().GETWITHDRAWS;
        }
        return this;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_layout_rv;
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv.getLoadMoreFooterView();
        mAdapter = new CurrentMonthFragmentAdapter(getActivity(), mData, type);
        rv.setIAdapter(mAdapter);
        rv.setOnLoadMoreListener(this);
        rv.setOnRefreshListener(this);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            mPage = 1;
            getData();
        }
    }

    /**
     * 获取数据
     */
    private void getData() {
        OkGo.<String>post(url)
                .params("m_number", MyApplication.getInstance().getUserId())
                .params("page", mPage)
                .execute(new MyStringCallBack<MonthListBean>(rv) {
                    @Override
                    public void onSuccessData(MonthListBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (mPage == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            if (mPage == 1) {
                                mData.clear();
                                mAdapter.notifyDataSetChanged();
                            }
                            endRefreshing(rv);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        mPage = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            mPage++;
            getData();
        }
    }

    private AdapterView.OnItemClickListener mItemClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            startActivity(new Intent(getActivity(), IncomeDetailActivity.class));
        }
    };
}